//# sourceURL=http://uMapApp/Module/EventAlert/EventAlertModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/Item/ListItem"
    , "./EventAlert"
    , "./EventCamera"
    , "./EventAlertBlock"
    , "../StakeNum/Mileage"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    ListBlock,
    PropertiesBlock
    , ListItem
    , EventAlert
    , EventCamera
    , EventAlertBlock
    , Mileage
) {

    function EventAlertModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "视频告警";
        _this.icon = _this.app.icons.camera;
        _this.panels = [];
        _this.items = [];
        _this.cameras = {};

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showMinMax": true, "title": "视频告警列表"});

        // if (_this.standalone) {
        //     _this.listPanel.showMinMax = true;
        // } else {
        //     _this.listPanel.showClose = true;
        // }

        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock EventAlertModule"});
        _this.listPanel.listBlock.title = _this.title;

        _this.listPanel.listBlock.eventTypes = new mhjs.Div({"class": "eventTypes"});
        _this.listPanel.listBlock.append(_this.listPanel.listBlock.eventTypes);

        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        // _this.detailPanel = new FloatPanel({"class": "leftPanel", "showClose": true});
        // _this.detailPanel.propertiesBlock = new PropertiesBlock({"class": "fullBlock"});
        // _this.detailPanel.append(_this.detailPanel.propertiesBlock);
        //
        // _this.detailPanel.on("open", function () {
        //     _this.trigger("openDetail");
        // });
        //
        // _this.detailPanel.on("close", function () {
        //     _this.trigger("closeDetail");
        //     _this.unActiveAllItem();
        // });
        //
        // _this.panels.push(_this.detailPanel);

        _this.app.ui.layout.right.append(_this.listPanel);
        // _this.app.ui.layout.left.append(_this.detailPanel);
    }

    var _class = EventAlertModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"             : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);

            },
            get: function () {
                return this._data;
            }
        },
        "renderList"       : {
            value: function (list) {

                var _this = this;

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    _this.dealAlarm(item);

                    if (item.eventType && _this.listPanel.listBlock.eventTypes.types[item.eventType]) {
                        _this.listPanel.listBlock.eventTypes.types[item.eventType].count.text(Number(_this.listPanel.listBlock.eventTypes.types[item.eventType].count.text()) + 1);
                    }


                    // if (item.stakeNum) {
                    //     item.stakeNum = new Mileage(item.stakeNum);
                    //     if (_this.app.modules.road.roadLineModule.getSampledRoadCenterPositions(item.stakeNum)) {
                    //         item.position = item.position || new uMap.Position(_this.app.modules.road.roadLineModule.getSampledRoadCenterPositions(item.stakeNum).position);
                    //     }
                    // }

                    // console.log(item.channelId);

                    if (item.channelId) {

                        //新摄像头
                        if (!_this.cameras[item.channelId]) {

                            var camera = {};

                            camera.app = _this.app;
                            camera.icon = _this.icon;
                            camera.channelId = item.channelId;
                            camera.channelName = item.channelName;

                            _this.setCameraPosition(camera);

                            // if (_this.extendInfos && _this.extendInfos[camera.channelId]) {
                            //
                            //     if (_this.extendInfos[camera.channelId].position) {
                            //         camera.position = _this.extendInfos[camera.channelId].position;
                            //     }
                            //
                            //     if (_this.extendInfos[camera.channelId].model) {
                            //         camera.model = _this.extendInfos[camera.channelId].model;
                            //     }
                            //
                            //     if (_this.extendInfos[camera.channelId].viewport) {
                            //         camera.viewport = _this.extendInfos[camera.channelId].viewport;
                            //     }
                            //
                            // }

                            var eventCamera = new EventCamera(camera);

                            _this.cameras[camera.channelId] = eventCamera;

                            item.camera = eventCamera;

                            _this.items.push(eventCamera);

                        } else {

                            item.camera = _this.cameras[item.channelId];

                        }

                    }


                    var eventAlert = new EventAlert(item);

                    eventAlert.listItem = new ListItem({
                        "title"     : eventAlert.title,
                        "subtitle"  : eventAlert.deviceName + " " + eventAlert.time,
                        "icon"      : _this.icon.base64,
                        "eventAlert": eventAlert
                    });

                    eventAlert.block = new EventAlertBlock({
                        properties: eventAlert
                    });

                    // eventAlert.on("click", function () {
                    //     _this.activeItem(eventAlert);
                    //     eventAlert.block.load();
                    //     _this.app.pop.target = eventAlert;
                    //     _this.app.pop.content = eventAlert.block;
                    //     _this.app.pop.position = eventAlert.position;
                    //     _this.app.pop.open();
                    // });
                    //
                    // eventAlert.on("close", function (eventAlert) {
                    //     if (_this.app.pop.target === eventAlert) {
                    //         _this.app.pop.close();
                    //     }
                    // });

                    eventAlert.listItem.on("click", function () {

                        _this.activeItem(eventAlert);

                        if (eventAlert.camera && eventAlert.camera.position) {
                            eventAlert.block.load();
                            _this.app.pop.target = eventAlert.camera;
                            _this.app.pop.content = eventAlert.block;
                            _this.app.pop.position = eventAlert.camera.position;
                            _this.app.pop.open();
                        }

                        eventAlert.listItem.addClass("active");
                        eventAlert.listItem.parent().animate({"scrollTop": eventAlert.listItem.dom[0].offsetTop});

                        // _this.detailPanel.propertiesBlock.title = camera["名称"];
                        // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                        // _this.detailPanel.propertiesBlock.properties = camera;
                        // _this.detailPanel.open();
                    });

                    eventAlert.listItem.on("hide", function (listItem) {
                        // listItem.eventAlert.close();
                        // eventAlert.camera && eventAlert.camera.updateEventCount();
                    });

                    eventAlert.listItem.on("show", function (listItem) {
                        // listItem.eventAlert.open();
                        // eventAlert.camera && eventAlert.camera.updateEventCount();
                    });

                    _this.listPanel.listBlock.add(eventAlert.listItem);

                    if (eventAlert.camera) {
                        eventAlert.camera.addEvents(eventAlert);
                        eventAlert.position = eventAlert.camera.position;

                    }

                });

                // console.log(_this.cameras);
                // console.log(_this.items);

                _this.listPanel.listBlock.filter = {
                    "param": "eventAlert.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "eventAlert.time",
                        "label": "时间"
                    }
                };

                _this.listPanel.listBlock.filter.on("update", function () {

                    $.each(_this.items, function (cameraIndex, camera) {
                        camera.updateEventCount();
                    });

                });

            }
        },
        "clear"            : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem && item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"             : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;
                    $.each(_this.items, function (i, item) {
                        item.open();
                    });
                    _this.listPanel.open(false);
                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"         : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({}, function (data, mapData) {
                        _this.app.modules.road.roadLineModule.loadInit(function () {

                            _this.loadPosition(function (extendJson) {

                                if (mapData) {
                                    _this.objMap = mapData.obj;
                                    _this.propertiesMap = mapData.properties;
                                    _this.dic = mapData.dic;
                                }

                                if (extendJson && extendJson.data) {
                                    _this.extendInfos = extendJson.data;
                                }

                                if (_this.menu) {

                                    if (!_this.menu.alert) {
                                        _this.menu.alert = new mhjs.Div({"class": "alert"});
                                        _this.menu.append(_this.menu.alert);
                                    }


                                    _this.menu.alert.text(data.length);

                                    //临时去除
                                    _this.menu.alert.hide();

                                }


                                if (_this.dic && _this.dic.eventType) {
                                    $.each(_this.dic.eventType, function (label, type) {

                                        var typeDom = new mhjs.Div({"class": "eventType"});
                                        var count = new mhjs.Div({"class": "count"});
                                        var unit = new mhjs.Div({"class": "unit"});
                                        unit.text("条");
                                        typeDom.count = new mhjs.Div({"class": "value"});
                                        typeDom.count.text("0");

                                        var title = new mhjs.Div({"class": "title"});
                                        title.text(type.title);

                                        typeDom.on("click", function () {
                                            $.each(_this.items, function (cameraIndex, camera) {

                                                $.each(camera.events, function (eventIndex, event) {

                                                    if (event.eventType && event.eventType === label) {
                                                        event.listItem.show();
                                                    } else {
                                                        event.listItem.hide();
                                                    }

                                                });

                                                camera.updateEventCount();

                                            });

                                            typeDom.siblings().removeClass("active");
                                            typeDom.addClass("active");

                                        });

                                        count
                                            .append(typeDom.count)
                                            .append(unit)
                                        ;

                                        typeDom
                                            .append(count)
                                            .append(title)
                                        ;

                                        _this.listPanel.listBlock.eventTypes.types = {};
                                        _this.listPanel.listBlock.eventTypes.types[label] = typeDom;
                                        _this.listPanel.listBlock.eventTypes.append(typeDom);
                                    });
                                }


                                _this.data = data;
                                _this.isLoaded = true;
                                callback && callback();


                            });


                        });
                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"        : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"            : {
            value: function () {
                var _this = this;
                _this.closePanel(false);
                _this.unActiveAllItem();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "activeItem"       : {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.active();
                item.camera && item.camera.layer && item.camera.layer.active();

                // item.listItem.addClass("active");
                // item.listItem.parent().animate({"scrollTop": item.listItem.dom[0].offsetTop});
            }
        },
        "unActiveAllItem"  : {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);

                    $.each(item.events, function (i, event) {
                        event.listItem.removeClass("active");
                    });

                    // item.listItem.removeClass("active");
                });
            }
        },
        "dealAlarm"        : {
            value: function (item) {
                var _this = this;

                if (item.alarmType && _this.dic && _this.dic.eventType && _this.dic.eventTypeGroups) {


                    $.each(_this.dic.eventTypeGroups, function (groupIndex, groupTypeO) {
                        $.each(groupTypeO.alarmTypes, function (typeIndex, typeO) {
                            if (typeO.alarmTypeId === item.alarmType) {
                                item.alarmTypeO = typeO;
                                item.alarmTypeGroup = groupTypeO;
                                return false;
                            }
                        });
                    });

                    if (item.alarmTypeGroup) {
                        $.each(_this.dic.eventType, function (name, eventTypeO) {
                            $.each(eventTypeO.groupIds, function (index, id) {
                                if (id === item.alarmTypeGroup.groupId) {
                                    item.eventType = name;
                                    return false;
                                }
                            });
                        });
                    }

                }

                if (item.alarmStatus && _this.dic && _this.dic.eventStatusCode) {
                    $.each(_this.dic.eventStatusCode, function (name, eventStatusCodeO) {
                        if (item.alarmStatus === eventStatusCodeO.code) {
                            item.alarmStatusText = eventStatusCodeO.title;
                            return false;
                        }
                    });
                }

            }
        },
        "loadPosition"     : {
            value: function (callback) {

                var _this = this;

                if (_this.positionRequest) {
                    _this.positionRequest.send({}, function (json) {
                        callback && callback(json);
                    });
                } else {
                    callback && callback();
                }
            }
        },
        "setCameraPosition": {
            value: function (camera) {

                var _this = this;

                if (_this.extendInfos) {


                    $.each(_this.extendInfos, function (channelId, cameraPosition) {

                        if (cameraPosition.channelName === camera.channelName) {

                            if (cameraPosition.position) {
                                camera.position = cameraPosition.position;
                            }

                            if (cameraPosition.model) {
                                camera.model = cameraPosition.model;
                            }

                            if (cameraPosition.viewport) {
                                camera.viewport = cameraPosition.viewport;
                            }

                            //根据静态数据读取视频流地址替代后台拉取地址
                            if (cameraPosition.video) {
                                camera.videoURL = cameraPosition.video;
                            }

                            return false;
                        }

                    });
                }
            }
        }
    });

    return _class;

});